﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomControlPractice.MVP
{
    public partial class PageView : Page, IView
    {
        private Presenter presenter;

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            presenter = new Presenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            presenter.LoadAllCases();
        }

        protected void OnSearchButtonClick(object sender, EventArgs e)
        {
            presenter.Search();
        }

        protected void OnGridViewSorting(object sender,GridViewSortEventArgs e)
        {
            ViewState["sortExpression"] = e.SortExpression;
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = Direction.Descending;
            }
            else
            {
                ViewState["sortDirection"] = (Direction)ViewState["sortDirection"] == Direction.Descending
                             ? Direction.Ascending
                             : Direction.Descending;
            }

            presenter.Sort();
        }

        #region IView members
        public void RenderCases(IEnumerable<Case> cases)
        {
            gridView.DataSource = cases;
            gridView.DataBind();
        }

        public SearchType SearchType
        {
            get
            {
                return (SearchType)Enum.Parse(typeof(SearchType), searchType.Text.Trim());
            }
        }

        public string SearchContent
        {
            get { return searchContent.Text.Trim(); }
        }

        public string SortExpression
        {
            get { return (string)ViewState["sortExpression"]; }
        }

        public Direction Direction
        {
            get { return (Direction)ViewState["sortDirection"]; }
        }

        #endregion



    }
}
